<?php


declare (strict_types=1);

namespace app\admin\controller\admin;

use app\admin\controller\Controller;
use app\admin\model\admin\User as AdminUserModel;
use app\store\model\store\User as StoreUserModel;
use app\admin\service\admin\Role as AdminRoleService;
use app\store\service\store\User as StoreUserService;
use Exception;

/**
 * 超管后台管理员控制器
 * Class User
 * @package app\store\controller
 */
class User extends Controller
{
    /**
     * 强制验证当前访问的控制器方法method
     * 例: [ 'login' => 'POST' ]
     * @var array
     */
    protected $methodRules = [
        'detail' => 'GET',
        'renew' => 'POST',
    ];

    /**
     * 列表
     * @return array
     * @throws DbException
     */
    public function all()
    {
        $model = new AdminUserModel;
        $list = $model->getAllList($this->request->param());
        return $this->renderSuccess(compact('list'));
    }

    public function list()
    {
        $model = new AdminUserModel;
        $list = $model->getList($this->request->param());
        return $this->renderSuccess(compact('list'));
    }

    /**
     * 添加管理员
     * @return array
     */
    public function add()
    {
        // 新增记录
        $model = new AdminUserModel;
        if ($model->add($this->postForm(), $this->admin)) {
            return $this->renderSuccess('添加成功');
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }

    /**
     * 编辑管理员
     * @return array
     */
    public function edit($apiId)
    {
        // 新增记录
        $model = AdminUserModel::detail($apiId);
        if ($model->edit($this->postForm())) {
            return $this->renderSuccess('编辑成功');
        }
        return $this->renderError($model->getError() ?: '编辑失败');
    }

    /**
     * 删除
     * @param int $apiId
     * @return array|bool
     */
    public function delete($apiId)
    {
        // 管理员详情
        $model = new AdminUserModel;
        if (!$model->batchSetDelete($apiId)) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess('删除成功');
    }

    /**
     * 更新当前管理员信息
     * @return array|string
     * @throws Exception
     */
    public function renew()
    {
        // 获取当前用户信息
        $model = AdminUserModel::detail($this->admin['user']['admin_user_id']);
        // 更新用户信息
        if ($model->renew($this->postForm())) {
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /**
     * 获取当前用户信息
     * @return array
     */
    public function detail()
    {
        $userInfo = AdminUserModel::detail($this->admin['user']['admin_user_id']);
        return $this->renderSuccess(['userInfo' => $userInfo]);
    }

    public function info()
    {
        return $this->renderSuccess([
            // 用户信息
            'userInfo' => AdminUserModel::detail($this->admin['user']['admin_user_id'], ['role', 'avatar_file']),
            // 菜单权限
            'roles' => AdminRoleService::getLoginPermissions(),
        ]);
    }
}
